import { getUrl } from '@/api/manage'
import config from '@/config/defaultSettings'
export function timeFix() {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome() {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent() {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader(callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () { }
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE() {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate(id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}

/**
 * 金额格式化
 * @param value number => '￥12 3456'
 */
export function formatMoney(num) {
  if (num === undefined || num === null) {
    return '￥ 0'
  }
  num = num.toString().replace(/\$|,/g, '')
  if (isNaN(num)) {
    num = '0'
  }
  // eslint-disable-next-line eqeqeq
  const sign = num == (num = Math.abs(num))
  num = Math.floor(num)
  let cents = num % 100
  num = Math.floor(num / 100).toString()
  if (cents < 10) {
    cents = '0' + cents
  }
  for (let i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
    num =
      num.substring(0, num.length - (4 * i + 3)) +
      ',' +
      num.substring(num.length - (4 * i + 3))
  }
  const value = (sign ? '' : '-') + num + '.' + cents

  return `￥ ${value}`.replace(/\B(?=(\d{4})+(?!\d))/g, ' ')
}

/**
 * 金额格式化还原
 * @param Number value '￥12 3456' => number
 */
export function unformatMoney(value) {
  return value.replace(/￥\s?|( *)/g, '')
}

// 下载附件
export function downloadAttachment(fileName) {
  // getUrl({ name: fileName }).then(res => {
  // const url = res.resData && res.resData.result
  const files=  fileName.split(',')
  if (files.length>1){
    let u=''
    for (const f of files ){
      u = config.uploadedUrlPrefix +f
      window.open(u)
    }
  }else {
    const url = config.uploadedUrlPrefix + fileName
    window.open(url)
  }
}
